<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="资源系统标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入资源系统标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="源码资源介绍" prop="resourceIntro">
        <el-input
          v-model="queryParams.resourceIntro"
          placeholder="请输入源码资源介绍"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售价格，设置为0则可免费观看 " prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入销售价格，设置为0则可免费观看 "
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="源码原价" prop="originPrice">
        <el-input
          v-model="queryParams.originPrice"
          placeholder="请输入源码原价"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资源类型  1 小程序 2 网站源码  3 源码资源 " prop="resourceType">
        <el-select v-model="queryParams.resourceType" placeholder="请选择资源类型  1 小程序 2 网站源码  3 源码资源 " clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="附件信息 下载和密码" prop="attachmentInfo">
        <el-input
          v-model="queryParams.attachmentInfo"
          placeholder="请输入附件信息 下载和密码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input
          v-model="queryParams.description"
          placeholder="请输入描述"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="显示排序" prop="sort">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入显示排序"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上架下架，0下架 1上架" prop="enable">
        <el-input
          v-model="queryParams.enable"
          placeholder="请输入上架下架，0下架 1上架"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态，草稿 审核 发表" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态，草稿 审核 发表" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['app:code:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['app:code:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['app:code:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['app:code:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="codeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="课程ID" align="center" prop="id" />
      <el-table-column label="技术ID" align="center" prop="techId" />
      <el-table-column label="分类ID" align="center" prop="subjectId" />
      <el-table-column label="资源系统标题" align="center" prop="title" />
      <el-table-column label="源码资源介绍" align="center" prop="resourceIntro" />
      <el-table-column label="销售价格，设置为0则可免费观看 " align="center" prop="price" />
      <el-table-column label="源码原价" align="center" prop="originPrice" />
      <el-table-column label="总课时" align="center" prop="lessonNum" />
      <el-table-column label="资源类型  1 小程序 2 网站源码  3 源码资源 " align="center" prop="resourceType" />
      <el-table-column label="附件信息 下载和密码" align="center" prop="attachmentInfo" />
      <el-table-column label="封面图片路径" align="center" prop="cover" />
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="销售数量" align="center" prop="buyCount" />
      <el-table-column label="浏览数量" align="center" prop="viewCount" />
      <el-table-column label="显示排序" align="center" prop="sort" />
      <el-table-column label="上架下架，0下架 1上架" align="center" prop="enable" />
      <el-table-column label="状态，草稿 审核 发表" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['app:code:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['app:code:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改源码资源对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="资源系统标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入资源系统标题" />
        </el-form-item>
        <el-form-item label="源码资源介绍" prop="resourceIntro">
          <el-input v-model="form.resourceIntro" placeholder="请输入源码资源介绍" />
        </el-form-item>
        <el-form-item label="销售价格，设置为0则可免费观看 " prop="price">
          <el-input v-model="form.price" placeholder="请输入销售价格，设置为0则可免费观看 " />
        </el-form-item>
        <el-form-item label="源码原价" prop="originPrice">
          <el-input v-model="form.originPrice" placeholder="请输入源码原价" />
        </el-form-item>
        <el-form-item label="资源类型  1 小程序 2 网站源码  3 源码资源 ">
          <el-select v-model="form.resourceType" placeholder="请选择资源类型  1 小程序 2 网站源码  3 源码资源 ">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="附件信息 下载和密码" prop="attachmentInfo">
          <el-input v-model="form.attachmentInfo" placeholder="请输入附件信息 下载和密码" />
        </el-form-item>
        <el-form-item label="封面图片路径" prop="cover">
          <el-input v-model="form.cover" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="显示排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入显示排序" />
        </el-form-item>
        <el-form-item label="上架下架，0下架 1上架" prop="enable">
          <el-input v-model="form.enable" placeholder="请输入上架下架，0下架 1上架" />
        </el-form-item>
        <el-form-item label="状态，草稿 审核 发表">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCode, getCode, delCode, addCode, updateCode, exportCode } from "@/api/app/code";

export default {
  name: "Code",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 源码资源表格数据
      codeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        techId: undefined,
        subjectId: undefined,
        title: undefined,
        resourceIntro: undefined,
        price: undefined,
        originPrice: undefined,
        lessonNum: undefined,
        resourceType: undefined,
        attachmentInfo: undefined,
        cover: undefined,
        description: undefined,
        buyCount: undefined,
        viewCount: undefined,
        sort: undefined,
        enable: undefined,
        status: undefined,
        remarks: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "资源系统标题不能为空", trigger: "blur" }
        ],
        cover: [
          { required: true, message: "封面图片路径不能为空", trigger: "blur" }
        ],
        description: [
          { required: true, message: "描述不能为空", trigger: "blur" }
        ],
        enable: [
          { required: true, message: "上架下架，0下架 1上架不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询源码资源列表 */
    getList() {
      this.loading = true;
      listCode(this.queryParams).then(response => {
        this.codeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        techId: undefined,
        subjectId: undefined,
        title: undefined,
        resourceIntro: undefined,
        price: undefined,
        originPrice: undefined,
        lessonNum: undefined,
        resourceType: undefined,
        attachmentInfo: undefined,
        cover: undefined,
        description: undefined,
        buyCount: undefined,
        viewCount: undefined,
        sort: undefined,
        enable: undefined,
        status: "0",
        remarks: undefined,
        updateTime: undefined,
        createTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加源码资源";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCode(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改源码资源";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateCode(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addCode(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除源码资源编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCode(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有源码资源数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCode(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
